﻿/**
 * @author Marcelo Volmaro
 */

package {	import flash.display.MovieClip;
	import flash.display.StageScaleMode;
	import flash.display.StageAlign;
	import flash.events.Event;
	import flash.utils.getDefinitionByName;
	import extremefx.GlobalStage;
	
	import @@classNameAndPath@@;
	
	[SWF(width="640", height="480", frameRate="30", backgroundColor="#FFFFFF")]
	public final class Preloader extends MovieClip{
		public function Preloader() {
            stop();
            stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
        }
        
        private function onEnterFrame(event:Event):void {
            graphics.clear();
            var percent:Number = loaderInfo.bytesLoaded / loaderInfo.bytesTotal;
            graphics.beginFill(0);
            graphics.drawRect(0, stage.stageHeight / 2 - 10, stage.stageWidth * percent, 20);
            graphics.endFill();
            
            if(percent >= 1) {
                removeEventListener(Event.ENTER_FRAME, onEnterFrame);
	        	if (stage) init();
				else addEventListener(Event.ADDED_TO_STAGE, init);
            }
        }
        
        private function init():void {
        	graphics.clear();
        	removeEventListener(Event.ADDED_TO_STAGE, init);

			new GlobalStage(stage);
            var MainClass:Class = getDefinitionByName("@@classNameAndPath@@") as Class;
			var mainClass:@@className@@ = new MainClass();
			parent.addChild(mainClass);
			mainClass.main();
			parent.removeChild(this);
        }	}}